//
//  messgeVC.swift
//  swift_airu
//
//  Created by A on 2025/9/19.
//

import UIKit
import CHTCollectionViewWaterfallLayout
import HandyJSON
class AUMessgeVC: AUBaseCollectionVC {

    var dataArr: [AURoomList] = []
    override func creatLayout() -> CHTCollectionViewWaterfallLayout {
        let layout = CHTCollectionViewWaterfallLayout()
        layout.columnCount = 2;
        layout.minimumColumnSpacing = 15;
        layout.minimumInteritemSpacing = 15
        layout.sectionInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
        return layout
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navBarHidden = false

    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        self.title = "hahahah"
        
        collectionView?.register(AUHomeCell.self, forCellWithReuseIdentifier: "cell")
        collectionView?.enableRefresh(pullDownAction: {
            self.loadRecommendList(isRefresh: true)

        }, pullUpAction: {
            self.loadRecommendList(isRefresh: false)

        }, autoBegin: true)

//        collectionView?.enableRefresh(pullDownAction: { [weak self] in
//           }, pullUpAction: { [weak self] in
//           })
           
     
    }
    private func loadRecommendList(isRefresh: Bool) {
        let header = ["Authorization": "Bearer 5bca2e2d-94bf-44a5-8bcb-ba6ff5a10b55"]
//        AUHud.show()
            NetworkManager.shared.request("admin/room/recommendList", model: AURoomList.self, headers: header) { result in
                // 默认只关注 data 是否存在
                if let roomArray = result.data as? [AURoomList], !roomArray.isEmpty {
                    if isRefresh {
                        self.dataArr.removeAll()
                        self.collectionView?.resetNoMoreData()
                    }
                    self.dataArr.append(contentsOf: roomArray)
                    self.collectionView?.reloadData()
                    self.collectionView?.endRefreshing()
                } else {
                    // data 为 nil 或空数组
                    if isRefresh { self.dataArr.removeAll() }
                    self.collectionView?.endRefreshing(noMoreData: true)
                    
                    // ✅ 只有在特定业务需要时才判断 code
                    let code = result.code
                    if code != 0 {
                        AUHud.toast("业务异常 code: \(code)")
                    }
                }
            } failure: { msg in
                AUHud.toast(msg)
                self.collectionView?.endRefreshing()
            }
    }
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArr.count
       }
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! AUHomeCell

        cell.configure(with: dataArr[indexPath.row])
        return cell
    }
    override func collectionView(_ collectionView: UICollectionView,
                                 layout collectionViewLayout: UICollectionViewLayout,
                                 sizeForItemAt indexPath: IndexPath) -> CGSize {

        guard let layout = collectionViewLayout as? CHTCollectionViewWaterfallLayout else {
            return CGSize(width: 0, height: 0)
        }

        let totalSpacing = layout.minimumColumnSpacing * CGFloat(layout.columnCount - 1)
            + layout.sectionInset.left + layout.sectionInset.right
        let width = (collectionView.bounds.width - totalSpacing) / CGFloat(layout.columnCount)

        let height = width + 50
        return CGSize(width: width, height: height)
    }
     func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        navigationController?.pushViewController(AUOneVC(), animated: true)
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
